using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using ProduceBasicData.Api.Application.Command.ProductionManage.Technology;
using ProduceBasicData.Api.DTO.ProductionManage;
using ProduceBasicData.Domain.Inventories;
using ProduceBasicData.Domain.ProductionManage;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.ProductionManage.Technology
{
    /// <summary>
    /// 获取工单生产任务状态查询处理器
    /// </summary>
    public class GetWorkOrderProcessStatusCommandHandler : IRequestHandler<GetWorkOrderProcessStatusCommand, ApiResult<Responsedto>>
    {
        private readonly IBaseRespository<Production_report> _productionReportRepository;
        private readonly IBaseRespository<Work_order> _workOrderRepository;
        private readonly IBaseRespository<CraftModel> _craftRepository;
        private readonly IMapper _mapper;

        public GetWorkOrderProcessStatusCommandHandler(
            IBaseRespository<Production_report> productionReportRepository,
            IBaseRespository<Work_order> workOrderRepository,
            IBaseRespository<CraftModel> craftRepository,
            IMapper mapper)
        {
            _productionReportRepository = productionReportRepository;
            _workOrderRepository = workOrderRepository;
            _craftRepository = craftRepository;
            _mapper = mapper;
        }

        public async Task<ApiResult<Responsedto>> Handle(GetWorkOrderProcessStatusCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 1. 验证工单是否存在
                var workOrder = await _workOrderRepository.GetModelAsync(request.OrderId);
                if (workOrder == null)
                {
                    return new ApiResult<Responsedto>()
                    {
                        Code = ApiEnum.Failed,
                        Msg = $"工单ID {request.OrderId} 不存在"
                    };
                }

                var order = _workOrderRepository.GetAll().Where(o => !o.Isdel);
                var productionReports = _productionReportRepository.GetAll().Where(o => !o.Isdel);
                var craft = _craftRepository.GetAll().Where(o => !o.Isdel);

                var info = from a in productionReports
                           join b in order on a.Order_id equals b.Id
                           join c in craft on a.Process_id equals c.Id
                           where b.Id==request.OrderId
                           select new Responsedto()
                           {
                               CrafId = c.Id,
                               CraftNo=c.CraftNo,
                               CraftName=c.CraftName,
                               PriviewJson=c.PriviewJson,
                               OrderId=b.Id,
                               Order_no=b.Order_no,
                               Status=b.Status,
                               Work_type=b.Work_type,
                           };

                return new ApiResult<Responsedto>()
                {
                    Code = ApiEnum.Success,
                    Msg = "查询工单生产任务状态成功",
                    Data = info.FirstOrDefault()
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<Responsedto>()
                {
                    Code = ApiEnum.Failed,
                    Msg = $"查询工单生产任务状态失败: {ex.Message}"
                };
            }
        }
    }
}
